package com.atguigu.srb.sms.controller.api;

import com.atguigu.srb.common.exception.Assert;
import com.atguigu.srb.common.result.R;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.common.util.RegexValidateUtils;
import com.atguigu.srb.sms.client.CoreUserInfoClient;
import com.atguigu.srb.sms.service.SmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/sms")
@Api("短信测试接口")
//@CrossOrigin
public class ApiSmsController {

    @Autowired
    SmsService smsService;

    @Autowired
    CoreUserInfoClient coreUserInfoClient;

    //测试sentinel
    @ApiOperation("流量测试接口")
    @GetMapping("/test")
    public R send(){
//        boolean b = coreUserInfoClient.checkMoblie("13233449287");
//        try {
//            Thread.sleep(600);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        return R.ok();
    }

    @ApiOperation("注册短信发送接口")
    @GetMapping("/send/{mobile}")
    public R send(@PathVariable("mobile") String mobile){

        boolean bMobile = RegexValidateUtils.checkCellphone(mobile);
        Assert.isTrue(bMobile, ResponseEnum.LOGIN_MOBILE_ERROR);

        //调用core服务，检查当前手机号是否被注册
        boolean b = coreUserInfoClient.checkMoblie(mobile);//通过feign调用core
        Assert.isTrue(!b,ResponseEnum.MOBILE_EXIST_ERROR);

        //调用业务层发送短信
        smsService.send(mobile);
        return R.ok();
    }
}
